package com.example.myprojectoffeixue;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

public class GateWay_Shezhi extends Activity {
    ArrayList<String> s;
    Spinner sp;
    String Mac;
    String name;
    Dialog dialog;
    private ArrayList<GateWay> user;
    MyApplication m;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gate_way__shezhi);
        info();
    }
    public void info(){
        LinearLayout tfinish = (LinearLayout) findViewById(R.id.iv_my_baojing);
        tfinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });




        sp = (Spinner) findViewById(R.id.sp_ato2);
        s = new ArrayList<>();
        m = (MyApplication) getApplication();
        //  ---------------------------
        UserClass uc = new UserClass();
        uc.userId = m.userId;
        uc.isAdmin = "1";
        String js = JSON.toJSONString(uc);
        Log.d("jsh",js);
        js = AESUtil.encrypt(js);
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.setContentType("application/json");
        try {
            params.setBodyEntity(new StringEntity(js, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        httpUtils.send(HttpRequest.HttpMethod.POST, HttpConnection.canAuthorizeList, params,
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        HttpConnection.toastLog("网络错误", GateWay_Shezhi.this);
                    }
                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        if (arg0.result != null && !("".equals(arg0.result))) {
                            arg0.result = AESUtil.decrypt(arg0.result);
                            Log.d("result",arg0.result);
                            JSONObject jb = JSONObject.parseObject(arg0.result);
                            String status = jb.getString("status");
                            if ("0".equals(status)) {
                                JSONArray ja = jb.getJSONArray("deviceList");
                                user = (ArrayList<GateWay>) JSON.parseArray(ja.toString(), GateWay.class);

                                for (int i = 0; i < user.size(); i++) {
                                    s.add(user.get(i).mac);
                                }
                                Spinneradapter sp5 = new Spinneradapter(s,  GateWay_Shezhi.this);
                                sp.setAdapter(sp5);

                            }else{
                                HttpConnection.toastLog("服务器错误", GateWay_Shezhi.this);
                            }
                        }else{
                            HttpConnection.toastLog("服务器错误",  GateWay_Shezhi.this);
                        }

                    }
                });


        sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (s.size() != 0) {
                    name = s.get(position);
                    Mac = user.get(position).mac;
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {

                if (s.size() != 0) {
                    Mac = s.get(0);
                    Mac = user.get(0).mac;
                }

            }


    });


        TextView tv_restart= (TextView) findViewById(R.id.restart);
        TextView tv_reset= (TextView) findViewById(R.id.reset);



        tv_restart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dialog = new Dialog(GateWay_Shezhi.this,R.style.CustomActivityDialogStyle);
                dialog.setContentView(R.layout.activity_dialog_all);
                dialog.show();
                dialog.setCanceledOnTouchOutside(false);
                TextView ok= (TextView) dialog.findViewById(R.id.tv_dialog);
                TextView no= (TextView) dialog.findViewById(R.id.tv2_dialog);
                TextView title= (TextView) dialog.findViewById(R.id.shebei_dialog);
                title.setText("是否重启网关？");


                ok.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        restart();
                        dialog.dismiss();
                    }
                });

                no.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });



            }
        });





        tv_reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog = new Dialog(GateWay_Shezhi.this,R.style.CustomActivityDialogStyle);
                dialog.setContentView(R.layout.activity_dialog_all);
                dialog.show();
                dialog.setCanceledOnTouchOutside(false);
                TextView ok= (TextView) dialog.findViewById(R.id.tv_dialog);
                TextView no= (TextView) dialog.findViewById(R.id.tv2_dialog);
                TextView title= (TextView) dialog.findViewById(R.id.shebei_dialog);
                title.setText("是否恢复出厂设置？");


                ok.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        reset();
                        dialog.dismiss();
                    }
                });
                no.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
            }
        });
}
    public void restart(){
        SheBeiClass sk = new SheBeiClass();
        sk.cmd="1004";
        sk.mac=Mac;
        String t = JSON.toJSONString(sk);
        if(m.flag3){
        m.c.send(t);}
    }


public void reset(){
    SheBeiClass sk = new SheBeiClass();
    sk.cmd="1003";
    sk.mac=Mac;
    String t = JSON.toJSONString(sk);
    if(m.flag3){
    m.c.send(t);}
}
}